<?php

class PrivacyController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	public function init() {
		$this->commonAction();	
	}
	//首页
	public function indexAction() {
		try {
						
			$count = 10;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
			//print_r($curpage);exit;
	     	if (! $curpage)
    				$curpage = 1;
			$this->view->curpage= $curpage; 
    		$offset = ($curpage - 1) * $count; 
			
			$result = $this->_dbAdapter->fetchAll ("select SQL_CALC_FOUND_ROWS *  from privacy order by id desc limit $offset,$count");
			//print_r($result);exit;
			
			$param ['file'] = '/' . $this->controller . '/' . $this->action;
			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
			$param ['perpagenum'] = $count; // 每页显示的数目
			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
			$param ['curpage'] = $curpage; // 当前页码
			$this->view->allnum=$param ['totalnum'];

			
			$id = ( int ) $this->_request->getParam ( 'id' );
			$current=array();
			if($result)
			foreach($result as $key => $value){
				if(empty($current)){
					$current=$value;
					$idselect=$value['id'];
				}
				
				if($id>0 && $id==$value['id']){
					$current=$value;
					$idselect=$value['id'];
				}
				
			}
			
			$this->view->id= $idselect; 
			
			
			$page = new MyClass_Page ( $param );
			$page->setvar ( array ('id' => $idselect) );
			$this->view->page= $page->getNumPage (); 
			
			$this->view->content= $current['content']; 
			$this->view->description= $current['description']; 
			$this->view->result= $result; 
		
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	public function opAction(){
		try {
	    	if (! $this->isPost ()) {
				throw new Exception ( '参数错误！' );
			}			
			$p=$this->_request->getPost();
			//print_r($p);exit;
			if($p['content']==''){
				throw new Exception ( '内容不能为空！' );
			}
			if($p['description']==''){
				throw new Exception ( '修改说明不能为空！' );
			}
			if($p['content'] != ''){
				$data=array(
					'content'=>$p['content'],
					'title'=>$p['description'],
					'addtime'=>time(),
				);
				$res=$this->_dbAdapter->Insert('privacy',$data);
				if ($res) {
					$this->generatehtml();
					$this->feedback ( '保存成功', 'success', '/privacy/index', 'tip', 3 );
				} else {
					throw new Exception ( '保存失败' );
				}		
			}
						
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}




	//生成html
	public function generatehtmlAction() {
		try {		
			
				$rshtml=$this->generatehtml();
				if($rshtml){
					$urls=PROJECT_ROOT .'/public/html/';
					//echo $urls.$thumbname_big;exit;
					$htmlname='privacy.html';
					$res=Azure_Storage::defaultImg('html',$urls.$htmlname,$htmlname);
					//print_r($res);exit;
				
					if($res){
						//删除本地文件
						unlink($urls.$htmlname);
						$this->feedback ( 'success', '生成html成功', '/'.$this->controller.'/index', 'tip', 3 );
					}else{
						throw new Exception ('生成失败');
					}
				}

		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function generatehtml() {
		try {
			$sql="select *  from privacy where 1 order by id desc";
			$result=$this->_dbAdapter->fetchRow($sql);
			if(!$result){
				return -2;
			}
			$result['time']=date('Y-m-d H:i',$result['addtime']);
			$result['id']=$result['id'];
			$result['headtitle']= '云中校车软件许可及服务协议';
			$result['imgurl']='';
			//print_r($result);exit;
		
			$this->whtml($result,'privacy','1');//1内容=array，2模版，3类别表示
			return 1;

		} catch ( Exception $e ) {
			return $e->getMessage ();
		}
	}
	
	//显示html
	public function htmlAction() {
		try {
			 $path='http://cloud.yunxiaoche.com/html/privacy.html'; 
		 
		     $html=file_get_contents($path);  
			 
	       $this->view->html= $html;
		   
	 } catch ( Exception $e ) {
		$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 
	 

	//显示html
	public function viewAction() {
		try {
			 $path='http://cloud.yunxiaoche.com/html/privacy.html'; 
		     echo $html=file_get_contents($path);  
			 exit;	 
	 } catch ( Exception $e ) {
		$this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	} 

	 
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

